<?php
class Notification {
	var $collection		= null;		
	var $cache			= null;
	var $clear			= null;
	protected $_operators = array(
									'<'   => '$lt',
									'>'   => '$gt',
									'<='  =>  '$lte',
									'>='  => '$gte',
									'!='  => array('single' => '$ne', 'multiple' => '$nin'),
									'<>'  => array('single' => '$ne', 'multiple' => '$nin'),
									'or'  => '$or',
									'||'  => '$or',
									'not' => '$not',
									'!'   =>  '$not'
								);
	/*
	------- table nofications
	------- field
	* userid
	* type: {
			--- admin ----
				1: report right; -1: report wrong
				2: divorced
				3: gift fullhouse - von. They're married
			--- friend ----
				11: req friend;
				12: req close friend;
				13: req lover;
				14: req propose
				15: accept friend; -15:deny friend
				16: accept close friend; -16: deny close friend
				17: accept lover; -17: deny lover
				18: accept propose; -18: deny propose
				19: remove friend
				20: remove lover
				21: req divorce
				22: accept divorce; -20: deny divorce
				23: cancel req divorce				
			--- give ----
				24: give gift
				25: accept / deny gift
				26: give von
				27: accept / deny von
				28: give like
				29: accept / deny like
			--- diary ----
				40: create
				41: news comment
				42: like diary
				43: like comment
			--- photo ----
				44: post news
				45: new comment
				46: like photo
				47: like cmt
				48: tag image
			--- full-house ----
				49: news blast
				50: news cmt
				51: like blast
				52: like cmt
			--- offline ----
				53: post offline
				54: news cmt
				55: reg offline
				56: invite offline
				57: accept invite offline; -57:deny invite offline
				58: accepted / denied offline
				59: host cancel offline
				60: user cancel
				61: like offline
				62: like cmt
			--- news ----
				63: like news
				64: plus news
				65: post cmt
				66: plus cmt
			--- place ----
				67: place cmt
				68: add image of place
			}
	* content: json {posterid;....}
	* time
	* isread: 0 unread|1:read
	* srcid: for delete
	*/
	public function __construct($dbName = DB_MONGO_VFRIEND) {
		if(DB_MONGO_ENABLE) {
			$mongo 				= new Mongo();
			$this->collection 	= $mongo->selectDB('vfriend')->selectCollection('notifications');	
			$this->cache		= new Vfriend_Cache();
			$this->clear		= $this->cache->clear;
		}
	}
	public function delete() {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove();
		}
	}
	public function deleteByUserId($intUserId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('userid'=>(int)$intUserId));
			$this->clear->deleteNotification($intUserId);
		}
	}
	public function deleteById($intId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('_id' => new MongoId($intId)), true);
		}
	}
	public function deleteByType($intType, $intSrcId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('type'=>$intType,'srcid'=>(int)$intSrcId));
		}
	}
	public function insert($intUserId, $intType, $strContent, $intSrcId=0) {	
		try {
			if(DB_MONGO_ENABLE) {
				$intAllowInsert = 1;
				// disable request
				if(($intType > 10 && $intType < 15) || $intType == 21) {
					$intAllowInsert = 0;
				}
				if($intType > 39) {
					$objVfriendUser 		= new Vfriend_User();
					$arrUserNotification	= $objVfriendUser->getUserNotification($intUserId);
					if($arrUserNotification) {
						switch(abs($intType)) {
							case 40:
								$intAllowInsert = (($arrUserNotification['bnotification']&(1<<0)) > 0) ? 1 : 0;
								break;
							case 41:
								$intAllowInsert = (($arrUserNotification['bnotification']&(1<<1)) > 0) ? 1 : 0;
								break;
							case 42:
								$intAllowInsert = (($arrUserNotification['bnotification']&(1<<2)) > 0) ? 1 : 0;
								break;
							case 43:
								$intAllowInsert = (($arrUserNotification['bnotification']&(1<<3)) > 0) ? 1 : 0;
								break;
							# image
							case 44:						
								$intAllowInsert = (($arrUserNotification['inotificaiton']&(1<<0)) > 0) ? 1 : 0;
								break;
							case 45:
								$intAllowInsert = (($arrUserNotification['inotificaiton']&(1<<1)) > 0) ? 1 : 0;
								break;
							case 46:
								$intAllowInsert = (($arrUserNotification['inotificaiton']&(1<<2)) > 0) ? 1 : 0;
								break;
							case 47:
								$intAllowInsert = (($arrUserNotification['inotificaiton']&(1<<3)) > 0) ? 1 : 0;
								break;
							case 48:
								$intAllowInsert = (($arrUserNotification['inotificaiton']&(1<<4)) > 0) ? 1 : 0;
								break;
							# full-house						
							case 49:
								$intAllowInsert = (($arrUserNotification['fnotification']&(1<<0)) > 0) ? 1 : 0;
								break;						
							case 50:
								$intAllowInsert = (($arrUserNotification['fnotification']&(1<<1)) > 0) ? 1 : 0;
								break;
							case 51:
								$intAllowInsert = (($arrUserNotification['fnotification']&(1<<2)) > 0) ? 1 : 0;
								break;
							case 52:
								$intAllowInsert = (($arrUserNotification['fnotification']&(1<<3)) > 0) ? 1 : 0;
								break;
							# offline							
							case 53:
							case 61:
								$intAllowInsert = (($arrUserNotification['onotification']&(1<<0)) > 0) ? 1 : 0;
								break;
							case 54:
							case 62:
								$intAllowInsert = (($arrUserNotification['onotification']&(1<<1)) > 0) ? 1 : 0;
								break;
							case 55:
								$intAllowInsert = (($arrUserNotification['onotification']&(1<<2)) > 0) ? 1 : 0;
								break;
							case 56:
								$intAllowInsert = (($arrUserNotification['onotification']&(1<<3)) > 0) ? 1 : 0;
								break;
							case 57:
								$intAllowInsert = (($arrUserNotification['onotification']&(1<<4)) > 0) ? 1 : 0;
								break;
							case 58:
								$intAllowInsert = (($arrUserNotification['onotification']&(1<<5)) > 0) ? 1 : 0;
								break;
							case 59:
							case 60:
								$intAllowInsert = (($arrUserNotification['onotification']&(1<<6)) > 0) ? 1 : 0;
								break;
							#place
							case 67:
								$intAllowInsert = (($arrUserNotification['pnotification']&(1<<0)) > 0) ? 1 : 0;
								break;
							case 68:
								$intAllowInsert = (($arrUserNotification['pnotification']&(1<<1)) > 0) ? 1 : 0;
								break;
						}
					} else {
						$intAllowInsert = 0;
					}
				}
				if($intAllowInsert == 1) {
					$objInsert = array(	'userid'	=> (int)$intUserId, 
										'type' 		=> $intType,
										'content'	=> (string)$strContent,
										'srcid'		=> (int)$intSrcId,
										'time'		=> time(),
										'isread'	=> 0
								);		
					$this->collection->insert($objInsert);
					$this->clear->deleteNotification($intUserId);
				}
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Notification::insert - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateUnread($intUserId) {
		try {		
			if(DB_MONGO_ENABLE) {
				$objWhere 	= array('isread'=>0,'userid'=>(int)$intUserId);
				$objUpdate	= array('isread'=>1);
				$objMulti	= array('multiple'=>true);
				$this->collection->update($objWhere, array('$set'=>$objUpdate),$objMulti);
				$this->clear->deleteNotification($intUserId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Notification::updateUnread - ' .$e->getMessage());			
			return null;
		}
	}
	public function getNotifyByUserId($intUserId, $intOffset=0, $intLimit=NOTIFICATION_LIMIT, &$intTotal=0) {
		try {			
			if(DB_MONGO_ENABLE) {
				$arrResult	= null;
				if($intOffset == 0) {
					$strKey		= NOTIFICATION_GETNOTIFYBYUSERID . $intUserId;
					$arrResult	= $this->cache->get($strKey);
				}
				if(!Vfriend_Function::isCached($arrResult) || 1) {
					$objFind 	= array('userid'=>(int)$intUserId);
					$objSort	= array('time'=>-1);
					$objCursor	= $this->collection->find($objFind);
					$arrData 	= $objCursor->sort($objSort)->limit((int)$intLimit)->skip((int)$intOffset);
					$intTotal	= $objCursor->count();					
					if((object)$arrData) {
						$arrAssignResult = $arrResult = null;
						foreach($arrData as $value) {
							//$value['postdate'] 	= date('Y-m-d H:i:s',$value['time']);
							$value['content']	= Zend_Json::decode($value['content']);
							$arrAssignResult[] 	= $value;
						}
						$arrResult['data'] 		= $arrAssignResult;
						$arrResult['total'] 	= $intTotal;
						if($intOffset == 0) {
							$this->cache->set($strKey, $arrResult, NOTIFICATION_GETNOTIFYBYUSERID_EXPIRE);
						}
					}
				}
				$intTotal = isset($arrResult['total']) ? $arrResult['total'] : 0;
				return isset($arrResult['data']) ? $arrResult['data'] : null;
			}
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Notification::getNotifyByUserId - ' .$e->getMessage());			
			return null;
		}
	}
	public function getNotifyUnread($intUserId) {
		try {
			if(DB_MONGO_ENABLE) {
				$strKey		= NOTIFICATION_GETNOTIFYUNREAD . $intUserId;
				$arrResult	= $this->cache->get($strKey);
				if(!Vfriend_Function::isCached($arrResult)) {
					$objFind 	= array('userid'=>(int)$intUserId,'isread'=>0);
					$objSort	= array('time'=>-1);
					$intCount 	= $this->collection->find($objFind)->count();
					$arrResult	= array('count'=>$intCount);
					$this->cache->set($strKey, $arrResult, NOTIFICATION_GETNOTIFYUNREAD_EXPIRE);
				}
			}
			return isset($arrResult['count']) ? $arrResult['count'] : 0;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Notification::getNotifyUnread - ' .$e->getMessage());			
			return null;
		}
	}	
}
?>
